package com.yoshino.usercenter.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoshino.usercenter.mapper.UserTeamMapper;
import com.yoshino.usercenter.service.UserTeamService;
import com.yoshino.yoshinoapicommon.model.entity.UserTeam;
import com.yoshino.yoshinoapicommon.model.vo.TeamUserVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【user_team(用户队伍关系)】的数据库操作Service实现
* @createDate 2022-12-26 13:49:44
*/
@Service
public class UserTeamServiceImpl extends ServiceImpl<UserTeamMapper, UserTeam>
    implements UserTeamService {


    @Override
    public List<TeamUserVO> teamMemberNum(List<TeamUserVO> teamList) {
        // 查询加入队伍的用户信息（人数）
        if (teamList == null || teamList.isEmpty()) {
            return teamList;
        }
        final List<Long> teamIdList = teamList.stream().map(TeamUserVO::getId).collect(Collectors.toList());
        QueryWrapper<UserTeam> userTeamJoinQueryWrapper = new QueryWrapper<>();
        userTeamJoinQueryWrapper.in("teamId", teamIdList);
        List<UserTeam> userTeamList = this.list(userTeamJoinQueryWrapper);
        // 队伍 id => 加入这个队伍的用户列表
        Map<Long, List<UserTeam>> teamIdUserTeamList = userTeamList.stream().collect(Collectors.groupingBy(UserTeam::getTeamId));
        teamList.forEach(team -> {
            team.setHasJoinNum(teamIdUserTeamList.getOrDefault(team.getId(), new ArrayList<>()).size());
        });
        return teamList;
    }

    @Override
    public Integer teamMemberNumByTeamId(Long teamId) {
        // 查询加入队伍的用户信息（人数）
        if (teamId == null || teamId < 0) {
            return 0;
        }
        QueryWrapper<UserTeam> userTeamJoinQueryWrapper = new QueryWrapper<>();
        userTeamJoinQueryWrapper.eq("teamId", teamId);
        List<UserTeam> userTeamList = this.list(userTeamJoinQueryWrapper);
        return userTeamList.size();
    }
}




